package com.ibeetl.admin.aod.service.excel.processor;

import com.ibeetl.admin.aod.entity.AodIndustry;
import com.ibeetl.admin.aod.entity.AodIndustryKeyword;
import com.ibeetl.admin.aod.service.AodIndustryKeywordService;
import com.ibeetl.admin.aod.service.AodIndustryService;
import com.ibeetl.admin.aod.web.dto.AodIndustryKeywordDto;
import com.ibeetl.admin.aod.web.query.AodIndustryQuery;
import com.ibeetl.admin.core.util.PlatformException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class AodIndustryKeywordImportProcessor extends AbstractImportProcessor<AodIndustryKeywordDto> {

    @Autowired
    protected AodIndustryKeywordService aodIndustryKeywordService;

    @Autowired
    protected AodIndustryService aodIndustryService;

    @Override
    protected void postProcess(List<AodIndustryKeywordDto> dataList) {

        List<AodIndustry> aodIndustryList = aodIndustryService.queryList(new AodIndustryQuery());
        Map<String,Long> aodIndustryMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(aodIndustryList)) {
            for (AodIndustry aodIndustry : aodIndustryList) {
                aodIndustryMap.put(aodIndustry.getIndustryName(), aodIndustry.getId());
            }
        }

        if (aodIndustryMap.size() == 0) {
            throw new PlatformException("行业类别为空,无法添加行业关键词");
        }
        // 改成批量插入 TODO
        for (AodIndustryKeywordDto aodIndustryKeywordDto : dataList) {
            if (aodIndustryMap.containsKey(aodIndustryKeywordDto.getIndustryName())) {
                // 找到了对应的就插入,没有就忽略 TODO
                AodIndustryKeyword aodIndustryKeyword = new AodIndustryKeyword();
                aodIndustryKeyword.setIndustryId(aodIndustryMap.get(aodIndustryKeywordDto.getIndustryName()));
                aodIndustryKeyword.setKeyword(aodIndustryKeywordDto.getKeyword());
                aodIndustryKeywordService.save(aodIndustryKeyword);
            }
        }
    }
}
